import React, { Component } from 'react';
import { pageTo, cardCache, toast } from 'nc-lightapp-front';
import api from '../../common/api';
import config from '../../pages/list/config';
import _ from 'lodash-es';

const { setDefData, getDefData } = cardCache;

class Modal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isApply: false
        };
    }

    show(isApply) {
        this.setState({
            isApply
        });
        this.props.modal.show('tradetype-modal');
        this.getTradeTypeList(isApply).then((data) => {
            this.props.table.setAllTableData(config.tradetypecode, data);
        });
    }

    async getTradeTypeList(isApply) {
        let data = getDefData(`tradetype-ifcz-${isApply ? 'true' : 'false'}`, config.cache);
        if (data) {
            return data;
        }
        data = await api.getTradeType({ ifcz: isApply ? 'true' : 'false' });
        setDefData(`tradetype-ifcz-${isApply ? 'true' : 'false'}`, config.cache, data.TradeModal);
        return data.TradeModal;
    }

    onModalOk() {
        const { table, multiLang, modal } = this.props;
        const checkedRows = table.getCheckedRows(config.tradetypecode);
        if (checkedRows.length !== 1) {
            toast({
                color: 'warning',
                content: multiLang['select_one_row']
            }); /* 国际化处理： 必须选择一个*/
            return false;
        }
        const record = _.get(checkedRows, '[0].data.values');
        if (this.state.isApply) {
            const tradeType = _.get(record, 'pk_billtypecode.value', '');
            const appcode = _.get(record, 'appcode.value', '');
            let pagecode = _.get(record, 'pagecode.value', '');

            if (tradeType === '264X-Cxx-bxld') {
                pagecode = '201102CZBX_C';
            }
            if (tradeType === '263X-Cxx-sqld') {
                pagecode = '201102CZJK_C';
            }

            if (tradeType.startsWith('264')) {
                pageTo.specialOpenTo(
                    `${config.bxApply.url}tradetype=${tradeType}&toappcode=${appcode}&topagecode=${pagecode}`,
                    {
                        ...config.bxApply.info
                    }
                );
            } else if (tradeType.startsWith('263')) {
                pageTo.specialOpenTo(
                    `${config.jkApply.url}tradetype=${tradeType}&toappcode=${appcode}&topagecode=${pagecode}`,
                    {
                        ...config.jkApply.info
                    }
                );
            } else {
                toast({
                    color: 'warning',
                    content: multiLang['wrong_trade_type']
                }); /* 国际化处理： 不支持参照申请的交易类型*/
            }
        } else {
            pageTo.specialOpenTo(_.get(record, 'url.value'), {
                pagecode: _.get(record, 'pagecode.value'),
                appcode: _.get(record, 'appcode.value'),
                status: 'add'
            });
        }
        modal.close('tradetype-modal');
    }
    onModalCancel() {
        const { modal, table } = this.props;
        modal.close('tradetype-modal');
        table.setAllTableData(config.tradetypecode, []);
    }
    onTableSelected(props, moduleId, record, index, selected) {
        const { table } = this.props;
        table.selectAllRows(moduleId, false);
        table.selectTableRows(moduleId, index, true);
    }

    renderModalContent() {
        const { table } = this.props;

        return table.createSimpleTable(config.tradetypecode, {
            showIndex: true,
            showCheck: true,
            onSelected: this.onTableSelected.bind(this)
        });
    }

    render() {
        const { modal, multiLang } = this.props;
        return modal.createModal('tradetype-modal', {
            userControl: true,
            title: multiLang['trade_type'],
            content: this.renderModalContent(),
            beSureBtnClick: this.onModalOk.bind(this),
            cancelBtnClick: this.onModalCancel.bind(this)
        });
    }
}

export default Modal;
